namespace DotNetNuke.Common.Lists
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using DotNetNuke.Services.Exceptions;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Data;
    using System.Runtime.CompilerServices;

    public class ListController
    {
        private const string ENTRY_LIST_CACHE_KEY = "Lists";

        public int AddListEntry(ListEntryInfo ListEntry)
        {
            bool EnableSortOrder = ListEntry.SortOrder > 0;
            return DataProvider.Instance().AddListEntry(ListEntry.ListName, ListEntry.Value, ListEntry.Text, ListEntry.ParentKey, EnableSortOrder, ListEntry.DefinitionID, ListEntry.Description);
        }

        public void DeleteList(string ListName, string ParentKey)
        {
            DataProvider.Instance().DeleteList(ListName, ParentKey);
        }

        public void DeleteListEntryByID(int EntryID, bool DeleteChild)
        {
            DataProvider.Instance().DeleteListEntryByID(EntryID, DeleteChild);
        }

        public void DeleteListEntryByListName(string ListName, string Value, bool DeleteChild)
        {
            DataProvider.Instance().DeleteListEntryByListName(ListName, Value, DeleteChild);
        }

        public ListInfo FillListInfo(string ListName, string ParentKey, int DefinitionID)
        {
            IDataReader dr = DataProvider.Instance().GetList(ListName, ParentKey, DefinitionID);
            ListInfo objListInfo = null;
            try
            {
                if (dr.Read())
                {
                    objListInfo = new ListInfo(Convert.ToString(RuntimeHelpers.GetObjectValue(dr["ListName"])));
                    ListInfo refObjectHelperL0 = objListInfo;
                    refObjectHelperL0.DisplayName = Convert.ToString(RuntimeHelpers.GetObjectValue(dr["DisplayName"]));
                    refObjectHelperL0.Level = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["Level"]));
                    refObjectHelperL0.DefinitionID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["DefinitionID"]));
                    refObjectHelperL0.Key = Convert.ToString(RuntimeHelpers.GetObjectValue(dr["Key"]));
                    refObjectHelperL0.EntryCount = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["EntryCount"]));
                    refObjectHelperL0.ParentID = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["ParentID"]));
                    refObjectHelperL0.ParentKey = Convert.ToString(RuntimeHelpers.GetObjectValue(dr["ParentKey"]));
                    refObjectHelperL0.Parent = Convert.ToString(RuntimeHelpers.GetObjectValue(dr["Parent"]));
                    refObjectHelperL0.ParentList = Convert.ToString(RuntimeHelpers.GetObjectValue(dr["ParentList"]));
                    refObjectHelperL0.EnableSortOrder = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["MaxSortOrder"])) > 0;
                    refObjectHelperL0 = null;
                }
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
                
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return objListInfo;
        }

        public ListEntryInfo GetListEntryInfo(int EntryID)
        {
            return (ListEntryInfo) CBO.FillObject(DataProvider.Instance().GetListEntries("", "", EntryID, -1), typeof(ListEntryInfo));
        }

        public ListEntryInfo GetListEntryInfo(string ListName, string Value)
        {
            return this.GetListEntryInfo(ListName, Value, "");
        }

        public ListEntryInfo GetListEntryInfo(string ListName, string Value, string ParentKey)
        {
            return (ListEntryInfo) CBO.FillObject(DataProvider.Instance().GetListEntriesByListName(ListName, Value, ParentKey), typeof(ListEntryInfo));
        }

        public ListEntryInfoCollection GetListEntryInfoCollection(string ListName)
        {
            return this.GetListEntryInfoCollection(ListName, "");
        }

        public ListEntryInfoCollection GetListEntryInfoCollection(string ListName, string Value)
        {
            return this.GetListEntryInfoCollection(ListName, Value, "");
        }

        public ListEntryInfoCollection GetListEntryInfoCollection(string ListName, string Value, string ParentKey)
        {
            IList objListEntryInfoCollection = new ListEntryInfoCollection();
            return (ListEntryInfoCollection) CBO.FillCollection(DataProvider.Instance().GetListEntriesByListName(ListName, Value, ParentKey), typeof(ListEntryInfo), ref objListEntryInfoCollection);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Get entry list(s)
        /// Optional parent key to specifed if result is all entries in that category or a subset
        /// </summary>
        /// <remarks>
        /// To be used with Method 2:
        /// This method query each array of entries when it needed 
        /// An option to specified if arraylist object will be stored in cache or not        
        /// pro: stored data in cache only if specified; don't have populate all entries into collection      
        /// con: call database more than previous method; store to cache need to be considered each time using this method
        /// </remarks>
        public ListInfo GetListInfo(string ListName)
        {
            return this.GetListInfo(ListName, "");
        }

        public ListInfo GetListInfo(string ListName, string ParentKey)
        {
            return this.GetListInfo(ListName, ParentKey, -1);
        }

        public ListInfo GetListInfo(string ListName, string ParentKey, int DefinitionID)
        {
            return this.FillListInfo(ListName, ParentKey, DefinitionID);
        }

        public ListInfoCollection GetListInfoCollection()
        {
            return this.GetListInfoCollection("");
        }

        public ListInfoCollection GetListInfoCollection(string ListName)
        {
            return this.GetListInfoCollection(ListName, "");
        }

        public ListInfoCollection GetListInfoCollection(string ListName, string ParentKey)
        {
            return this.GetListInfoCollection(ListName, ParentKey, -1);
        }

        public ListInfoCollection GetListInfoCollection(string ListName, string ParentKey, int DefinitionID)
        {
            IList objListInfoCollection = new ListInfoCollection();
            ListInfo objListInfo = new ListInfo();
            return (ListInfoCollection) CBO.FillCollection(DataProvider.Instance().GetList(ListName, ParentKey, DefinitionID), typeof(ListInfo), ref objListInfoCollection);
        }

        public void UpdateListEntry(ListEntryInfo ListEntry)
        {
            DataProvider.Instance().UpdateListEntry(ListEntry.EntryID, ListEntry.ListName, ListEntry.Value, ListEntry.Text, ListEntry.Description);
        }

        public void UpdateListSortOrder(int EntryID, bool MoveUp)
        {
            DataProvider.Instance().UpdateListSortOrder(EntryID, MoveUp);
        }

        public enum CacheScope
        {
            None,
            Lists,
            ListCollection
        }
    }
}

